﻿Imports Publics
Imports System.Windows.Forms

Module ModuleNhapLieu

#Region "Set filter trên danh mục"

    Public Sub SetFilterConditionMa(ByRef _gridView As DanhMuc.VmsGridView, ByRef _column As DevExpress.XtraGrid.Columns.GridColumn, ByVal _fieldName As String, Optional ByVal _value As String = "", Optional ByVal _laChon As Boolean = False)
        'Thiết lập điều kiện lọc cho tất cả các cột trên gridview
        _gridView.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Default
        For Each item As DevExpress.XtraGrid.Columns.GridColumn In _gridView.Columns
            If _column.Equals(item) = False Then
                item.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains
            End If
            'Nếu là cột chọn thì không cho phép filter
            If item.FieldName.ToLower = "chon" Then
                item.OptionsFilter.AllowAutoFilter = False
                item.OptionsFilter.AllowFilter = False
            End If
        Next
        'Thiết lập điều kiện lọc lại cho riêng cột mã
        If New clsBienMoiTruong().PH_DM_LocCotMa = DieuKienLocCotMa.StartWith Then
            _column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Default
            If _value <> "" AndAlso _value <> "*" Then
                _gridView.Columns(_fieldName).FilterInfo = New DevExpress.XtraGrid.Columns.ColumnFilterInfo("[" & _fieldName & "] like '" & _value & "%'")
            End If
        Else
            _column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains
            If _value <> "" AndAlso _value <> "*" Then
                _gridView.Columns(_fieldName).FilterInfo = New DevExpress.XtraGrid.Columns.ColumnFilterInfo("[" & _fieldName & "] like '%" & _value & "%'")
            End If
        End If
        '
        If _value <> "" AndAlso _value <> "*" Then
            For i = 0 To _gridView.RowCount - 1
                Dim ma = _gridView.GetRowCellValue(i, _column)
                If ma IsNot Nothing AndAlso ma.ToString.StartsWith(_value) Then
                    _gridView.FocusedRowHandle = i
                    Return
                End If
            Next
        End If
        'Nếu không thấy row focus thì Focus cell lọc mã
        If _laChon Then
            _gridView.FocusedColumn = _column
            _gridView.FocusedRowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle
        End If
    End Sub

    Public Sub SetFilterConditionMa(ByRef _gridView As Publics.VmsGridView, ByRef _column As DevExpress.XtraGrid.Columns.GridColumn, ByVal _fieldName As String, Optional ByVal _value As String = "", Optional ByVal _laChon As Boolean = False)
        'Thiết lập điều kiện lọc cho tất cả các cột trên gridview
        _gridView.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Default
        For Each item As DevExpress.XtraGrid.Columns.GridColumn In _gridView.Columns
            If _column.Equals(item) = False Then
                item.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains
            End If
            'Nếu là cột chọn thì không cho phép filter
            If item.FieldName.ToLower = "chon" Then
                item.OptionsFilter.AllowAutoFilter = False
                item.OptionsFilter.AllowFilter = False
            End If
        Next
        'Thiết lập điều kiện lọc lại cho riêng cột mã
        If New clsBienMoiTruong().PH_DM_LocCotMa = DieuKienLocCotMa.StartWith Then
            _column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Default
            If _value <> "" AndAlso _value <> "*" Then
                _gridView.Columns(_fieldName).FilterInfo = New DevExpress.XtraGrid.Columns.ColumnFilterInfo("[" & _fieldName & "] like '" & _value & "%'")
            End If
        Else
            _column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains
            If _value <> "" AndAlso _value <> "*" Then
                _gridView.Columns(_fieldName).FilterInfo = New DevExpress.XtraGrid.Columns.ColumnFilterInfo("[" & _fieldName & "] like '%" & _value & "%'")
            End If
        End If
        '
        If _value <> "" AndAlso _value <> "*" Then
            For i = 0 To _gridView.RowCount - 1
                Dim ma = _gridView.GetRowCellValue(i, _column)
                If ma IsNot Nothing AndAlso ma.ToString.StartsWith(_value) Then
                    _gridView.FocusedRowHandle = i
                    Return
                End If
            Next
        End If
        'Nếu không thấy row focus thì Focus cell lọc mã
        If _laChon Then
            _gridView.FocusedColumn = _column
            _gridView.FocusedRowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle
        End If
    End Sub
#End Region

#Region "Lấy thông tin phím tắt"
    ''' <summary>
    ''' Lấy thông tin phím tắt cơ bản: 
    ''' Thêm, sửa, xóa, nạp lại, xuất dữ liệu, trợ giúp, đóng
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetLabelPhimTatCoBan(ByVal TenDanhMuc As String, ByVal Them As Boolean, ByVal Sua As Boolean, ByVal Xoa As Boolean) As String
        'Dim str As String = "Danh mục " & TenDanhMuc 'Bỏ cái này đi. Caption form có tên danh mục rồi cho cái này làm cái quái j nữa
        Dim str As String = "Phím tắt "
        If TenDanhMuc = "sản phẩm" Then
            str &= IIf(Them, GetStrKey(kThem), "") + IIf(Sua, GetStrKey(kSua), "") + IIf(Them, GetStrKey(kCopy), "") + IIf(Xoa, GetStrKey(kXoa), "") + _
                   GetStrKey(kLamTuoi) + GetStrKey(kXuat) + GetStrKey(kTroGiup) + GetStrKey(kThoat)
            'Bỏ: GetStrKey(kTimKiem) + 
        Else
            str &= IIf(Them, GetStrKey(kThem), "") + IIf(Sua, GetStrKey(kSua), "") + IIf(Xoa, GetStrKey(kXoa), "") + _
                   GetStrKey(kLamTuoi) + GetStrKey(kXuat) + GetStrKey(kTroGiup) + GetStrKey(kThoat)

        End If
        Return str
    End Function

    ''' <summary>
    ''' Lấy thông tin phím tắt cơ bản form cập nhật sản phẩm: 
    ''' Ghi và thêm, ghi và đóng, xóa, trợ giúp, đóng
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetLabelPhimTatCoBanCapNhatDanhMucSanPhamLapRap() As String
        Return GetStrKey(kGhiVaThem) + GetStrKey(kGhiVaDong) + GetStrKey(kXoa) + GetStrKey(kTroGiup) + GetStrKey(kThoat)
    End Function

    ''' <summary>
    ''' Lấy thông tin phím tắt cơ bản form cập nhật sản phẩm: 
    ''' Ghi và thêm, ghi và đóng, trợ giúp, đóng
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetLabelPhimTatCoBanCapNhatDanhMucCoGhiVaThem() As String
        Return GetStrKey(kGhiVaThem) + GetStrKey(kGhiVaDong) + GetStrKey(kTroGiup) + GetStrKey(kThoat)
    End Function

    ''' <summary>
    ''' Lấy thông tin phím tắt cơ bản form cập nhật: 
    ''' Ghi và đóng, trợ giúp, đóng
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetLabelPhimTatCoBanCapNhatDanhMuc() As String
        Return GetStrKey(kGhiVaDong) + GetStrKey(kTroGiup) + GetStrKey(kThoat)
    End Function

    ''' <summary>
    ''' Lấy thông tin phím tắt cơ bản form cập nhật: 
    ''' Ghi và thêm, Ghi và đóng, trợ giúp, đóng
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetLabelPhimTatCoBanCapNhatDonDat() As String
        Return GetStrKey(kGhiVaThem) + GetStrKey(kGhiVaDong) + GetStrKey(kTroGiup) + GetStrKey(kThoat)
    End Function
#End Region

    Public Function GetStrKey(ByVal Phim As clsPhimTat) As String
        Try
            If Phim Is Nothing Then Return ""
            Dim strCaptionKey As String = ""
            Select Case Phim.MaNut
                Case pIdThem
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnAddNewCaption + "]"
                Case pIdSua
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnEditCaption + "]"
                Case pIdXoa
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnDeleteCaption + "]"
                Case pIdChon
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnAcceptCaption + "]"
                Case pIdXuat
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnExportDataCaption + "]"
                Case pIdThoat
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnCloseCaption + "]"
                Case pIdTimKiem
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnSearchCaption + "]"
                Case pIdTimTiep

                Case pIdGhiVaDong
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnSaveAndCloseCaption + "]"
                Case pIdGhiVaThem
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnSaveAndAddCaption + "]"
                Case pIdIn
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnPrintCaption + "]"
                Case pIdTroGiup
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnHelpCaption + "]"
                Case pIdCopy
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnCopyCaption + "]"
                Case pIdLamTuoi
                    strCaptionKey = " [" + Trim(Phim.TenNut) + ": " + _
                                    BtnRefreshCaption + "]"
            End Select
            Return strCaptionKey
        Catch ex As Exception
            MsgInfo(ex.Message)
            Return Nothing
        End Try
    End Function

    Public Function GetInfoKey(ByVal maPhim As String) As clsPhimTat
        Dim Key As New clsPhimTat
        Try
            Key = (From db In vlstPhimTat Where db.MaNut = maPhim Select db).FirstOrDefault
        Catch ex As Exception
            MsgInfo(ex.Message)
        End Try
        Return Key
    End Function

    Public kThem As clsPhimTat = GetInfoKey(pIdThem)
    Public kSua As clsPhimTat = GetInfoKey(pIdSua)
    Public kXoa As clsPhimTat = GetInfoKey(pIdXoa)
    Public kCopy As clsPhimTat = GetInfoKey(pIdCopy)
    Public kChon As clsPhimTat = GetInfoKey(pIdChon)
    Public kXuat As clsPhimTat = GetInfoKey(pIdXuat)
    Public kThoat As clsPhimTat = GetInfoKey(pIdThoat)
    Public kTimKiem As clsPhimTat = GetInfoKey(pIdTimKiem)
    Public kTimTiep As clsPhimTat = GetInfoKey(pIdTimTiep)
    Public kGhiVaDong As clsPhimTat = GetInfoKey(pIdGhiVaDong)
    Public kGhiVaThem As clsPhimTat = GetInfoKey(pIdGhiVaThem)
    Public kIn As clsPhimTat = GetInfoKey(pIdIn)
    Public kLamTuoi As clsPhimTat = GetInfoKey(pIdLamTuoi)
    Public kTroGiup As clsPhimTat = GetInfoKey(pIdTroGiup)

      

    ''' <summary>
    ''' Tính tiền: thành tiền, tiền CK, tiền thuế, tiền trước thuế, tiền sau thuế(Hóa đơn nhập)
    ''' </summary>
    ''' <param name="paramHDNhap_ChiTiet"></param>
    ''' <remarks></remarks>
    Public Sub CapNhatTienThanhToan(ByRef paramHDNhap_ChiTiet As cls_VT_HDNhap_ChiTiet, Optional ByVal intTTHD As Integer = -1)
        '
        If paramHDNhap_ChiTiet.SoLuong Is Nothing Then paramHDNhap_ChiTiet.SoLuong = 0
        If paramHDNhap_ChiTiet.DonGia Is Nothing Then paramHDNhap_ChiTiet.DonGia = 0
        If paramHDNhap_ChiTiet.TiLeCK Is Nothing Then paramHDNhap_ChiTiet.TiLeCK = 0
        If paramHDNhap_ChiTiet.ThueSuat Is Nothing Then paramHDNhap_ChiTiet.ThueSuat = 0
        '@Mục đích: Tính tiền chiết khấu, trước thuế, sau thuế...
        'Dim objBMT As New clsBienMoiTruongPH
        'Dim PH_HD_ListKhoBienDongGia = objBMT.PH_HD_ListKho.Trim.Split(",").ToList
        Dim intTinhTienHD As Integer
        If intTTHD = -1 Then
            intTinhTienHD = 0 'objBMT.PH_HD_TinhTienHD
        Else
            intTinhTienHD = intTTHD
        End If

        Dim vTienThanhToanHD As Integer = 1 'objBMT.PH_HD_TienThanhToanHD

        Dim TiLeCK As Decimal = 0
        Dim DonGia As Decimal = 0
        Dim SoLuong As Decimal = 0
        Dim ThueSuat As Decimal = 0
        Dim intTien As Decimal = 0 'Tien = DonGia * SoLuong

        If paramHDNhap_ChiTiet.TiLeCK.GetValueOrDefault <> 0 Then
            TiLeCK = CDec(paramHDNhap_ChiTiet.TiLeCK)
            If paramHDNhap_ChiTiet.TienCK.GetValueOrDefault = 0 Then
                paramHDNhap_ChiTiet.TienCK = Math.Round(CDec(paramHDNhap_ChiTiet.TiLeCK * paramHDNhap_ChiTiet.DonGia * paramHDNhap_ChiTiet.SoLuong / 100), 0, MidpointRounding.AwayFromZero)
            End If
        Else
            paramHDNhap_ChiTiet.TienCK = 0
        End If
        If paramHDNhap_ChiTiet.DonGia IsNot Nothing Then
            DonGia = CDec(paramHDNhap_ChiTiet.DonGia)
        End If
        If paramHDNhap_ChiTiet.SoLuong IsNot Nothing Then
            SoLuong = CDec(paramHDNhap_ChiTiet.SoLuong)
        End If
        If paramHDNhap_ChiTiet.ThueSuat IsNot Nothing Then
            ThueSuat = Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) 'Lấy trị tuyệt đối vì có thể thuế < 0
        End If

        intTien = Math.Round(DonGia * SoLuong, 0, MidpointRounding.AwayFromZero)    'Tien khong phan biet truoc hay sau thue
        paramHDNhap_ChiTiet.ThanhTien = intTien

        Select Case intTinhTienHD
            Case 0  ' TH1: Tính chiết khấu rồi tính thuế
                If paramHDNhap_ChiTiet.IsTruocThue Then ' San pham la truoc thue
                    ' ===>  Công thức bên Fox đây :-s
                    ' m_giav = Round(m_giab*(1-m_tlck*0.01), 0) && Don Gia von
                    ' m_thuesuat = Abs(m_thuesuat)
                    ' m_thanhtien = m_slhd*m_giab && = Gia SAU (chua tinh thue) =  Bia_tt : Thanh tien bia
                    ' m_thue = Round(m_thanhtien * m_thuesuat*0.01,0) && = ThueTGGT
                    ' m_tienhang = m_thanhtien && + m_thue && = Giaban ** nghia sua 11/12/2009 : ve Ha noi thoi, chan vai te
                    ' m_ck = Round(m_tienhang*m_tlck*0.01, 0) && = ckhau_tt : Chiet khau tinh thanh tien

                    paramHDNhap_ChiTiet.TienHang = Math.Round(intTien, 0, MidpointRounding.AwayFromZero) - Math.Round(CDec(paramHDNhap_ChiTiet.TienCK), 0, MidpointRounding.AwayFromZero)  'Tien truoc thue la tien chua duoc tinh thue day ma :(
                    paramHDNhap_ChiTiet.TienThue = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) / 100), 0, MidpointRounding.AwayFromZero) 'Tien thue
                    If vTienThanhToanHD = 0 Then    ' Bang 0 thi khong cong TienThue
                        paramHDNhap_ChiTiet.TienThanhToan = paramHDNhap_ChiTiet.TienHang
                    Else        ' Khac 0 thi cong TienThue
                        paramHDNhap_ChiTiet.TienThanhToan = paramHDNhap_ChiTiet.TienHang + paramHDNhap_ChiTiet.TienThue
                    End If
                    '//Tính tiền CK + Giá vốn
                    '//Giá vốn = Giá bìa * (1-TiLeCK/100)

                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)                  'Tien chiet khau
                    paramHDNhap_ChiTiet.GiaVon = Math.Round(CDec(DonGia * (1 - TiLeCK / 100)), 5, MidpointRounding.AwayFromZero)
                    paramHDNhap_ChiTiet.TienGiaVon = Math.Round(paramHDNhap_ChiTiet.GiaVon.GetValueOrDefault * SoLuong, 0, MidpointRounding.AwayFromZero)
                     

                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If
                Else     ' San pham la sau thue
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)  'Tien chiet khau
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If

                    '//Tiền SAU THUẾ(Đã có thuế)
                    paramHDNhap_ChiTiet.TienThanhToan = Math.Round(intTien, 0, MidpointRounding.AwayFromZero) - Math.Round(CDec(paramHDNhap_ChiTiet.TienCK), 0, MidpointRounding.AwayFromZero)
                    '//Tiền TRƯỚC THUẾ(Chưa thuế)
                    paramHDNhap_ChiTiet.TienHang = Math.Round(CDec(paramHDNhap_ChiTiet.TienThanhToan / (1 + ThueSuat / 100)), 0, MidpointRounding.AwayFromZero)
                    '//Tiền THUẾ = Tiền SAU THUẾ - Tiền TRƯỚC THUẾ
                    paramHDNhap_ChiTiet.TienThue = paramHDNhap_ChiTiet.ThanhTien - paramHDNhap_ChiTiet.TienHang
                    '// Tien gia von
                    paramHDNhap_ChiTiet.TienGiaVon = paramHDNhap_ChiTiet.TienHang
                    If paramHDNhap_ChiTiet.SoLuong.GetValueOrDefault <> 0 Then
                        paramHDNhap_ChiTiet.GiaVon = Math.Round(paramHDNhap_ChiTiet.TienGiaVon.GetValueOrDefault / paramHDNhap_ChiTiet.SoLuong.GetValueOrDefault, 5, MidpointRounding.AwayFromZero)
                    Else
                        paramHDNhap_ChiTiet.GiaVon = 0
                    End If

                End If
                '----------------------------------------------------------------------
                'TH2: Cần phải xem xét lại??????
            Case 1  ' TH2: Tính thuế xong mới tính CK
                If paramHDNhap_ChiTiet.IsTruocThue Then
                    ' truoc thue giong thang TH1 chu nhi? vi no dau co dong j den thue dau :(
                    paramHDNhap_ChiTiet.TienHang = intTien    'Tien truoc thue la tien chua duoc tinh thue day ma :(
                    paramHDNhap_ChiTiet.TienThue = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) / 100), 0) 'Tien thue
                    If vTienThanhToanHD = 0 Then    ' Bang 0 thi khong cong TienThue
                        paramHDNhap_ChiTiet.ThanhTien = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang), 0, MidpointRounding.AwayFromZero)
                    Else        ' Khac 0 thi cong TienThue
                        paramHDNhap_ChiTiet.ThanhTien = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang), 0, MidpointRounding.AwayFromZero) + Math.Round(CDec(paramHDNhap_ChiTiet.TienThue), 0, MidpointRounding.AwayFromZero)
                    End If
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)  'Tien chiet khau
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If
                Else
                    paramHDNhap_ChiTiet.ThanhTien = intTien   'Tien sau thue
                    paramHDNhap_ChiTiet.TienHang = Math.Round(CDec(paramHDNhap_ChiTiet.ThanhTien / (1 + paramHDNhap_ChiTiet.ThueSuat / 100)), 0) 'Tien truoc thue
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * TiLeCK / 100), 0, MidpointRounding.AwayFromZero)
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If

                    paramHDNhap_ChiTiet.TienThue = paramHDNhap_ChiTiet.ThanhTien - paramHDNhap_ChiTiet.TienHang
                End If
        End Select
    End Sub

    ''' <summary>
    ''' Tính tiền: thành tiền, tiền CK, tiền thuế, tiền trước thuế, tiền sau thuế(Hóa đơn nhập)
    ''' </summary>
    ''' <param name="paramHDNhap_ChiTiet"></param>
    ''' <remarks></remarks>
    Public Sub CapNhatTienThanhToan(ByRef paramHDNhap_ChiTiet As VT_HDNhap_ChiTiet, Optional ByVal intTTHD As Integer = -1)
        '
        If paramHDNhap_ChiTiet.SoLuong Is Nothing Then paramHDNhap_ChiTiet.SoLuong = 0
        If paramHDNhap_ChiTiet.DonGia Is Nothing Then paramHDNhap_ChiTiet.DonGia = 0
        If paramHDNhap_ChiTiet.TiLeCK Is Nothing Then paramHDNhap_ChiTiet.TiLeCK = 0
        If paramHDNhap_ChiTiet.ThueSuat Is Nothing Then paramHDNhap_ChiTiet.ThueSuat = 0
        '@Mục đích: Tính tiền chiết khấu, trước thuế, sau thuế...
        'Dim objBMT As New clsBienMoiTruongPH
        'Dim PH_HD_ListKhoBienDongGia = objBMT.PH_HD_ListKho.Trim.Split(",").ToList
        Dim intTinhTienHD As Integer
        If intTTHD = -1 Then
            intTinhTienHD = 0 'objBMT.PH_HD_TinhTienHD
        Else
            intTinhTienHD = 0 'intTTHD
        End If

        ' Có những đơn vị cộng thuế vào tiền trước thuế đối với trường hợp thuế < 0
        ' Có những đơn vị lại không cộng => LỞM
        ' Nên phải dùng biến môi trường
        Dim vTienThanhToanHD As Integer = 1 'objBMT.PH_HD_TienThanhToanHD

        Dim TiLeCK As Decimal = 0
        Dim DonGia As Decimal = 0
        Dim SoLuong As Decimal = 0
        Dim ThueSuat As Decimal = 0
        Dim intTien As Int64    'Tien = DonGia * SoLuong

        If paramHDNhap_ChiTiet.TiLeCK IsNot Nothing Then
            TiLeCK = CDec(paramHDNhap_ChiTiet.TiLeCK)
        End If
        If paramHDNhap_ChiTiet.DonGia IsNot Nothing Then
            DonGia = CDec(paramHDNhap_ChiTiet.DonGia)
        End If
        If paramHDNhap_ChiTiet.SoLuong IsNot Nothing Then
            SoLuong = CDec(paramHDNhap_ChiTiet.SoLuong)
        End If
        If paramHDNhap_ChiTiet.ThueSuat IsNot Nothing Then
            ThueSuat = Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) 'Lấy trị tuyệt đối vì có thể thuế < 0
        End If

        intTien = Math.Round(DonGia * SoLuong, 0)    'Tien khong phan biet truoc hay sau thue
        paramHDNhap_ChiTiet.ThanhTien = intTien

        Select Case intTinhTienHD
            '------------------------------------------------------------------------------------------------
            ' TH1: Tính chiết khấu rồi tính thuế
            '------------------------------------------------------------------------------------------------
            Case 0
                If paramHDNhap_ChiTiet.IsTruocThue Then ' San pham la truoc thue
                    ' ===>  Công thức bên Fox đây :-s
                    ' m_giav = Round(m_giab*(1-m_tlck*0.01), 0) && Don Gia von
                    ' m_thuesuat = Abs(m_thuesuat)
                    ' m_thanhtien = m_slhd*m_giab && = Gia SAU (chua tinh thue) =  Bia_tt : Thanh tien bia
                    ' m_thue = Round(m_thanhtien * m_thuesuat*0.01,0) && = ThueTGGT
                    ' m_tienhang = m_thanhtien && + m_thue && = Giaban ** nghia sua 11/12/2009 : ve Ha noi thoi, chan vai te
                    ' m_ck = Round(m_tienhang*m_tlck*0.01, 0) && = ckhau_tt : Chiet khau tinh thanh tien
                    paramHDNhap_ChiTiet.TienHang = intTien    'Tien truoc thue la tien chua duoc tinh thue day ma :(
                    paramHDNhap_ChiTiet.TienThue = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) / 100), 0, MidpointRounding.AwayFromZero) 'Tien thue
                    If vTienThanhToanHD = 0 Then    ' Bang 0 thi khong cong TienThue
                        paramHDNhap_ChiTiet.TienThanhToan = paramHDNhap_ChiTiet.TienHang
                    Else        ' Khac 0 thi cong TienThue
                        paramHDNhap_ChiTiet.TienThanhToan = paramHDNhap_ChiTiet.TienHang + paramHDNhap_ChiTiet.TienThue
                    End If

                    'Tính tiền CK + Giá vốn
                    'Giá vốn = Giá bìa * (1-TiLeCK/100)
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)                  'Tien chiet khau
                    paramHDNhap_ChiTiet.GiaVon = Math.Round(CDec(DonGia * (1 - TiLeCK / 100)), 5, MidpointRounding.AwayFromZero)
                    paramHDNhap_ChiTiet.TienGiaVon = Math.Round(paramHDNhap_ChiTiet.GiaVon.GetValueOrDefault * SoLuong, 0, MidpointRounding.AwayFromZero)
                    
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If
                Else     ' San pham la sau thue
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)  'Tien chiet khau
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If

                    '//Tiền SAU THUẾ(Đã có thuế)
                    paramHDNhap_ChiTiet.TienThanhToan = intTien - paramHDNhap_ChiTiet.TienCK
                    '//Tiền TRƯỚC THUẾ(Chưa thuế)
                    paramHDNhap_ChiTiet.TienHang = Math.Round(CDec(paramHDNhap_ChiTiet.TienThanhToan / (1 + ThueSuat / 100)), 0, MidpointRounding.AwayFromZero)
                    '//Tiền THUẾ = Tiền SAU THUẾ - Tiền TRƯỚC THUẾ
                    paramHDNhap_ChiTiet.TienThue = paramHDNhap_ChiTiet.TienThanhToan - paramHDNhap_ChiTiet.TienHang
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.GiaVon = Math.Round(CDec(DonGia * (1 - TiLeCK / 100) / (1 + ThueSuat / 100)), 5, MidpointRounding.AwayFromZero)
                    paramHDNhap_ChiTiet.TienGiaVon = Math.Round(paramHDNhap_ChiTiet.GiaVon.GetValueOrDefault * SoLuong, 0, MidpointRounding.AwayFromZero)
                    'Else
                    'paramHDNhap_ChiTiet.TienGiaVon = paramHDNhap_ChiTiet.TienHang
                    'paramHDNhap_ChiTiet.GiaVon = paramHDNhap_ChiTiet.TienGiaVon / paramHDNhap_ChiTiet.SoLuong
                    'End If
                End If
                '------------------------------------------------------------------------------------------------
                ' TH2: Cần phải xem xét lại??????
                '------------------------------------------------------------------------------------------------
            Case 1  ' TH2: Tính thuế xong mới tính CK
                If paramHDNhap_ChiTiet.IsTruocThue Then
                    '* Truong hop nay Giavon = m_tienhang + m_thue
                    'm_giav = Round(m_giab*(1-m_tlck*0.01), 0) && Don Gia von
                    '                    m_thuesuat = Abs(m_thuesuat)
                    'm_thanhtien = Round(m_slhd*m_giab,0) && = Gia SAU (chua tinh thue) =  Bia_tt : Thanh tien bia
                    'm_thue = Round(m_thanhtien * m_thuesuat*0.01,0) && = ThueTGGT
                    'm_tienhang = m_thanhtien && + m_thue && = Giaban	** nghia sua 11/12/2009 : ve Ha noi thoi, chan vai te
                    'm_ck = Round(m_tienhang*m_tlck*0.01, 0) && = ckhau_tt : Chiet khau tinh thanh tien
                    'If mact$"PHUTHO" And tlck=0 And ckhau_tt>0 &&AnhPN nhap tien chiet khau
                    '                        m_ck = ckhau_tt
                    '                    End If
                    '                    If mact = "KONTUM" Then
                    '                        m_tienhang = m_thanhtien - m_ck
                    '                        m_thue = Round(m_tienhang * m_thuesuat * 0.01, 0)
                    '                    End If
                    '* Truong hop nay Giavon = m_tienhang + m_thue
                    'm_giav = Round(m_giab*(1-m_tlck*0.01), 0) && Don Gia von
                    ' truoc thue giong thang TH1 chu nhi? vi no dau co dong j den thue dau :(
                    paramHDNhap_ChiTiet.TienHang = intTien    'Tien truoc thue la tien chua duoc tinh thue day ma :(
                    paramHDNhap_ChiTiet.TienThue = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * Math.Abs(CDec(paramHDNhap_ChiTiet.ThueSuat)) / 100), 0, MidpointRounding.AwayFromZero) 'Tien thue
                    If vTienThanhToanHD = 0 Then    ' Bang 0 thi khong cong TienThue
                        paramHDNhap_ChiTiet.ThanhTien = paramHDNhap_ChiTiet.TienHang
                    Else        ' Khac 0 thi cong TienThue
                        paramHDNhap_ChiTiet.ThanhTien = paramHDNhap_ChiTiet.TienHang + paramHDNhap_ChiTiet.TienThue
                    End If
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(intTien * TiLeCK / 100, 0, MidpointRounding.AwayFromZero)  'Tien chiet khau
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If
                Else
                    paramHDNhap_ChiTiet.ThanhTien = intTien   'Tien sau thue
                    paramHDNhap_ChiTiet.TienHang = Math.Round(CDec(paramHDNhap_ChiTiet.ThanhTien / (1 + paramHDNhap_ChiTiet.ThueSuat / 100)), 0, MidpointRounding.AwayFromZero) 'Tien truoc thue
                    'If paramHDNhap_ChiTiet.isNhapTiLeCK = True Then
                    paramHDNhap_ChiTiet.TienCK = Math.Round(CDec(paramHDNhap_ChiTiet.TienHang * TiLeCK / 100), 0, MidpointRounding.AwayFromZero)
                    'End If
                    If paramHDNhap_ChiTiet.TienCK Is Nothing Then
                        paramHDNhap_ChiTiet.TienCK = 0
                    End If

                    paramHDNhap_ChiTiet.TienThue = paramHDNhap_ChiTiet.ThanhTien - paramHDNhap_ChiTiet.TienHang
                End If
        End Select

    End Sub


    Public Function GetSourceHDNhapVT(ByVal objHoaDon As VT_HDNhap) As List(Of cls_VT_HDNhap_ChiTiet)
        Dim lstRe As New List(Of cls_VT_HDNhap_ChiTiet)
        'Lấy dữ liệu thô
        Dim lstHDNhap_CT = (From ds In dbTV.VT_HDNhap_ChiTiets Where ds.InUsed AndAlso ds.IDHDNhap = objHoaDon.ID _
                            Join dsVT In dbTV.VT_DM_VatTus On ds.IDVatTu Equals dsVT.ID Where dsVT.InUsed _
                            Join dsKho In dbTV.DM_Khos On ds.IDKho Equals dsKho.ID Where dsKho.InUsed _
                            Select ds.STT, ds.SoHDTC, ds.NgayHoaDon, dsKho.MaKho, dsKho.TenKho, dsVT.MaVatTu, dsVT.TenVatTu, dsVT.DVT, ds.DonGia, dsVT.GiaBan, ds.ThueSuat, _
                            ds.SoLuong, ds.TiLeCK, ds.TienCK, ds.ThanhTien, ds.TienHang, ds.TienThue, ds.TienThanhToan, ds.GiaVon, ds.TienGiaVon).ToList
        ' 
        For Each item In lstHDNhap_CT
            Dim objAdd As New cls_VT_HDNhap_ChiTiet
            With objAdd
                .STT = item.STT.GetValueOrDefault
                .SoHDTC = item.SoHDTC
                .NgayHoaDon = item.NgayHoaDon
                .MaKho = item.MaKho
                .TenKho = item.TenKho
                .MaVatTu = item.MaVatTu
                .TenVatTu = item.TenVatTu

                .DonGia = item.DonGia.GetValueOrDefault
                .ThueSuat = item.ThueSuat.GetValueOrDefault
                .DVT = item.DVT
                .SoLuong = item.SoLuong.GetValueOrDefault
                .ThanhTien = item.ThanhTien
                .TiLeCK = item.TiLeCK.GetValueOrDefault
                .TienCK = item.TienCK.GetValueOrDefault
                .TienThue = item.TienThue.GetValueOrDefault
                If item.TiLeCK.GetValueOrDefault = 0 AndAlso item.TienCK.GetValueOrDefault <> 0 Then
                    .TiLeCK = Math.Round(CDec(item.TienCK * 100 / item.ThanhTien), 2, MidpointRounding.AwayFromZero)
                End If
                '
                .TienThanhToan = item.TienThanhToan.GetValueOrDefault
                .TienHang = item.TienHang.GetValueOrDefault
            End With
            lstRe.Add(objAdd)
        Next
        '
        If (From ds In lstRe Where ds.STT = 0 Select ds).Count > 0 Then
            Dim iSTT As Integer = 1
            For Each item In lstRe
                item.STT = iSTT
                iSTT += 1
            Next
        End If
        '
        Return lstRe
    End Function

    
    Public Sub InHoaDonNhapVT(ByVal objIn As VT_HDNhap)
        Dim pathReportPhatHanh As String = ""
        Dim rpt As New rpt_Default_HDNhapVT
        Dim strPath = pathReportPhatHanh & "\" & "rpt_Default_HDNhapVT.repx"
        Try
            rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        Catch ex As Exception
            rpt = New rpt_Default_HDNhapVT
        End Try
        rpt.objHoaDonNhap = objIn
        Dim frm1 As New FrmCustomReport(rpt, strPath, FrmCustomReport.HoaDonNhapVatTu)
        frm1.WindowState = FormWindowState.Maximized
        frm1.ShowDialog()

    End Sub
End Module
